var express = require('express');
var router = express.Router();
var {paperModel,clazzModel,studentModel,stageModel,resultModel} = require('../model/model')

/* GET home page. */


router.get('/gettest', async (req, res) => {
  let { page, name, time } = req.query;
  console.log(page);
  let arr = [];
  let title = new RegExp(name);
  if (title) {
    arr.push({ name: title });
  }
  if (time) {
    let now = new Date().getTime();
    console.log(now);
    if (time === 'wait') {
      arr.push({
        'time.1': { $gte: now } // 'time.1' 指的是数组中的结束时间
      });
    } else if (time === 'equal') {
      arr.push({
        'time.0': { $lte: now }, // 'time.0' 指的是数组中的开始时间
        'time.1': { $gt: now }   // 'time.1' 指的是数组中的结束时间
      });
    } else if (time === 'complete') {
      arr.push({
        'time.1': { $lt: now } // 'time.1' 指的是数组中的结束时间
      });
    }
  }
  let data = await paperModel.find({ $and: arr })
    .skip((page - 1) * 5)
    .limit(5)
    .populate('class')
    .populate('questions');
  let total = await paperModel.countDocuments({ $and: arr });
  
  res.send({
    code: 200,
    msg: '获取成功',
    data: data,
    total: total
  });
});

//获取学生
router.get('/getstudent',async(req,res)=>{
  let {page,pageSize,name,tid,sex}= req.query
  let arr = []
  let title = new RegExp(name)
  if(title) {
    arr.push({ name: title });
  }
  if(tid) {
    arr.push({ tid: tid });
  }
  if(sex){
    arr.push({ sex: sex });
  }
  let data =  await studentModel.find({$and:arr}).populate('clazz').skip((page-1)*pageSize).limit(pageSize)
  let total = await studentModel.countDocuments()
  res.send({
    code:200,
    msg:'获取成功',
    data:data,
    total:total
  })
})

router.post('/addtest',(req,res)=>{
    paperModel.create(req.body)
    res.send({
      code:200,
      msg:'添加成功'
    })
})
router.post('/addclass',(req,res)=>{
    clazzModel.create(req.body)
    res.send({
      code:200,
      msg:'添加成功'
    })
})
//修改状态
router.post("/upflag",async(req,res)=>{
  let {id,flag} = req.body
  console.log(id,flag);
  await paperModel.updateOne({_id:id},{flag})
  res.send({
    code:200,
    msg:'修改成功'
  })
})
//查看试卷结果
router.get('/getresult',async(req,res)=>{
  let data =  await resultModel.find()
  res.send({
    code:200,
    msg:'获取成功',
    data:data,
  })
})

// 阶段
router.get("/getstage",async (req,res)=>{
  let data = await stageModel.find()
  res.send({
    code:200,
    msg:'获取成功',
    data:data,
  })
})
//获取班级
router.get('/getclazz',async(req,res)=>{
  let data =  await clazzModel.find()
  res.send({
    code:200,
    msg:'获取成功',
    data:data,
  })
})


router.post("/addresult",(req,res)=>{
  let arr = [
    {
      student:"66d44aadcb5ed8a150512e56",
      paper:"66d027ab6c92e4a48bf10198",
      result:[
        {
          
        }
      ],
      time:[1723334400,1723766400],
      grade:99,
      status:false,
      clazz:"66d1bdcdf18b5f162cd52980"
    }
  ]
  resultModel.create(arr)
  res.send({
    code:200,
    msg:'添加成功'
  })
})
module.exports = router;
